//
//  SwiftyJSONObject.swift
//  cabbage_serialization
//
//  Created by dfsx6 on 2021/9/6.
//

import UIKit
import SwiftyJSON

//MARK:-
class SwiftyJSONObject: NSObject {

    enum ListItemMode: Int {
        case one    = 0
        case big    = 1
        case more   = 2
        case zero   = 3
    }

    public var id: Int?
    public var columnName: String?
    public var publishTime: Date?
    public var listItemMode: SwiftyJSONObject.ListItemMode?

    init(fromJson json: JSON) {
        id = json["id"].intValue
        columnName = json["column_name"].stringValue
        
        let time = json["publish_time"].doubleValue
        publishTime = Date.init(timeIntervalSince1970: time)

        let listmode = json["list_item_mode"].intValue
        listItemMode = ListItemMode.init(rawValue: listmode)

    }
}

//MARK:-
extension SwiftyJSONObject {
    
    override var description: String {
        var des = ""
        des += "\n id = \(String(describing: id))"
        des += "\n columnName = \(String(describing: columnName))"
        des += "\n publishTime = \(String(describing: publishTime))"
        des += "\n listItemMode = \(String(describing: listItemMode))"

        return des
    }

    
    public static func serialization() {
        
        guard let url = Bundle.main.url(forResource: "content", withExtension: "json") else {
            print("read cms json error")
            return
        }
        
        do {
            let data = try Data.init(contentsOf: url)
            serialization(from: data)
        } catch let error {
            print(error.localizedDescription)
        }

    }
    
    public static func serialization(from data: Data) {
        
        let json = JSON.init(data)
        let object = SwiftyJSONObject.init(fromJson: json)
        print(object)
    }

}
